package com.financing.inceptor;
import com.financing.service.user.TokenService;
import com.financing.util.Json;
import com.financing.util.Str;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;

public class TokenInceptor implements Interceptor {

	private TokenService token = new TokenService();
	
	@Override
	public void intercept(Invocation inv) {
		Controller c = inv.getController();
		String tk = c.getPara("token","");
		Ret ret = token.check(tk);
		if(Str.nb(tk)) {
			if(ret.isOk()){
				c.setAttr("user", token.findUserByToken(tk));
				inv.invoke();
			}else{
				c.renderJson(Json.error(ret.getStr("msg")));
			}
		}else{
			c.renderJson(Json.error("TOKEN不可为空！"));
		}
	}
}